package com.stalltea.leetcode;

/**
 * @author yilvchangfa
 * @since 2021/12/3 17:15
 **/
public class AddTwoNumbers {

    public static void main(String[] args) {
        ListNode l1 = new ListNode(9);
        l1.next = new ListNode(9);
        ListNode l2 = new ListNode(1);
        l2.next = new ListNode(2);
        ListNode result = addTwoNumbers3(l1, l2);
        System.out.println(result);
    }

    // addTwoNumbers2优化
    public static ListNode addTwoNumbers3(ListNode l1, ListNode l2) {
        int carry = 0;
        ListNode result = null;
        ListNode next = new ListNode();
        while (null != l1 || null != l2) {
            int first = null == l1 ? 0 : l1.val;
            int second = null == l2 ? 0 : l2.val;
            int total = carry + first + second;
            carry = total / 10;
            next.next = new ListNode(total % 10);
            next = next.next;
            if (null == result) {
                result = next;
            }
            if (null != l1) {
                l1 = l1.next;
            }
            if (null != l2) {
                l2 = l2.next;
            }
        }
        if (carry == 1) {
            next.next = new ListNode(carry);
        }

        return result;
    }

    // 代码复杂
    public static ListNode addTwoNumbers2(ListNode l1, ListNode l2) {
        int addP = 0;
        ListNode result = null;
        ListNode next = new ListNode();
        while (!(null == l1 && null == l2) || addP > 0) {
            int first = 0;
            int second = 0;
            if (null != l1) {
                first = l1.val;
                l1 = l1.next;

            }
            if (null != l2) {
                second = l2.val;
                l2 = l2.next;
            }
            int total = addP + first + second;
            addP = total / 10;
            next.next = new ListNode(total % 10);
            next = next.next;
            if (null == result) {
                result = next;
            }
        }
        return result;
    }

    // 会有溢出异常的方式
    //    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
    //        StringBuilder first = new StringBuilder();
    //        StringBuilder second = new StringBuilder();
    //        while (null != l1) {
    //            first.insert(0, l1.val);
    //            l1 = l1.next;
    //        }
    //        while (null != l2) {
    //            second.insert(0, l2.val);
    //            l2 = l2.next;
    //        }
    //        Long f = Long.valueOf(first.toString());
    //        Long s = Long.valueOf(second.toString());
    //        long total = f + s;
    //        String tt = String.valueOf(total);
    //        char[] chars = tt.toCharArray();
    //
    //        ListNode firstNode = null;
    //        ListNode nextNode = null;
    //        for (int i = chars.length - 1; i > -1; i--) {
    //            int val = Integer.parseInt(String.valueOf(chars[i]));
    //            if (null == firstNode) {
    //                firstNode = new ListNode(val);
    //                nextNode = firstNode;
    //            } else {
    //                nextNode.next = new ListNode(val);
    //                nextNode = nextNode.next;
    //            }
    //
    //        }
    //        return firstNode;
    //    }

    static class ListNode {

        int val;

        ListNode next;

        public ListNode() {
        }

        public ListNode(int val) {
            this.val = val;
        }

        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

}
